package com.huangyi.adminservice.house.controller;

import com.huangyi.adminapi.house.domain.dto.SearchHouseListReqDTO;
import com.huangyi.adminapi.house.feign.HouseFeignClient;
import com.huangyi.adminservice.house.domain.dto.*;
import com.huangyi.adminapi.house.domain.vo.HouseDetailVO;
import com.huangyi.adminservice.house.domain.vo.HouseVO;
import com.huangyi.adminservice.house.service.IHouseService;
import com.huangyi.commoncore.domain.dto.BasePageDTO;
import com.huangyi.commoncore.utils.BeanCopyUtil;
import com.huangyi.commondomain.domain.R;
import com.huangyi.commondomain.domain.vo.BasePageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 房源管理
 */
@Slf4j
@RestController
@RequestMapping("/house")
public class HouseController implements HouseFeignClient {

    @Autowired
    private IHouseService houseService;

    /**
     * 新增或编辑房源
     * @param reqDTO 新增或编辑房源DTO
     * @return 房源ID
     */
    @PostMapping("/add_edit")
    public R<Long> addOrEdit(@Validated @RequestBody HouseAddOrEditReqDTO reqDTO) {
        Long result = houseService.addOrEdit(reqDTO);
        return R.ok(result);
    }

    @GetMapping("/detail")
    @Override
    public R<HouseDetailVO> detail(Long houseId) {
        HouseDTO houseDTO = houseService.detail(houseId);
        if(houseDTO == null) {
            log.warn("要查询的房源信息不存在 houseId:{}", houseId);
            return R.fail("房源信息不存在！");
        }
        HouseDetailVO houseDetailVO =houseDTO.convertToVO();
        return R.ok(houseDetailVO);
    }

    /**
     * 查询房源列表 (支持筛选, 支持翻页)
     * @param reqDTO 列表查询条件实体类
     * @return 房源列表
     */
    @PostMapping("/list")
    public R<BasePageVO<HouseVO>> list(@Validated @RequestBody HouseListReqDTO reqDTO) {
        BasePageDTO<HouseDescDTO> houseList = houseService.list(reqDTO);
        BasePageVO<HouseVO> result = new BasePageVO<>();
        BeanUtils.copyProperties(houseList, result);
        return R.ok(result);
    }

    /**
     * 更新房源状态信息
     * @param reqDTO 房源状态信息DTO
     * @return void
     */
    @PostMapping("/status/edit")
    public R<Void> editStatus(@Validated @RequestBody HouseStatusEditReqDTO reqDTO) {
        houseService.editStatus(reqDTO);
        return null;
    }

    /**
     * 刷新全量房源缓存
     */
    @GetMapping("/refresh")
    public R<Void> refreshHouseIds(){
        houseService.refreshHouseIds();
        return R.ok();
    }

    /**
     * 查询房源列表，支持筛选、排序、翻页
     */
    @Override
    public R<BasePageVO<HouseDetailVO>> searchList(SearchHouseListReqDTO searchHouseListReqDTO) {
        BasePageVO<HouseDetailVO> result = new BasePageVO<>();
        BasePageDTO<HouseDTO> searchDTO = houseService.searchList(searchHouseListReqDTO);
        result.setTotals(searchDTO.getTotals());
        result.setTotalPages(searchDTO.getTotalPages());
        result.setList(BeanCopyUtil.copyListProperties(searchDTO.getList(),HouseDetailVO::new));
        return R.ok(result);
    }
}
